package com.qingxin.inspection.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.qingxin.cms.entity.InspectionTask;
import com.qingxin.sys.dao.Querier;
import com.qingxin.sys.dao.Updater;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.BaseService;
import com.qingxin.sys.support.JsTreeNode;
import com.qingxin.sys.utils.BeanUtil;
import com.qingxin.sys.utils.StringUtils;
@Service
public class InspectionTaskService extends BaseService<InspectionTask, String>{
	
	/**
	 * 获取巡查任务菜单树数据
	 * @param loginUser
	 * @param isParent
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<JsTreeNode> getUITreeData(){
		List<JsTreeNode> list = new ArrayList<JsTreeNode>();
		StringBuilder hql = new StringBuilder();
		hql.append("select new com.qingxin.cms.entity.InspectionTask(taskId,inspecName)");
		hql.append(" from InspectionTask");
		
		hql.append(" order by createTime asc");
		List<InspectionTask> inspectionTasks = listByHql(hql.toString());
		if(inspectionTasks != null){
			for(InspectionTask inspectionTask : inspectionTasks){
				list.add(new JsTreeNode(inspectionTask.getTaskId(),inspectionTask.getInspecName(),"inspect_ITEM",null,false));
			}
		}
		return list;
	}

	/**
	 * 获取巡查任务分页数据
	 * @param pageParam
	 * @param search
	 * @param request
	 * @return
	 */
	public Pager list(PageParam pageParam,String search,HttpServletRequest request){
		Querier querier = Querier.create("select new com.qingxin.cms.entity.InspectionTask(" +
				"t.taskId,t.inspecCode,t.inspecName,t.remark,t.taskdesc,t.status,t.createTime)" +                              
				"from InspectionTask t");
		if(StringUtils.isNotEmpty(search)){
			search = "%"+search.replace("'", "")+"%";
			querier.append(" where (t.inspecName like :inspecName)");
			querier.setParam("inspecName", search);
		}
		querier.append(pageParam.getOrderby(" order by t.createTime desc","t"));
		Pager pager = pageByQuerier(querier, pageParam);
		return pager;
	}
	

	
	/**
	 * 删除巡查任务信息
	 * @param taskid
	 * @param request
	 * @return
	 */
	public SysError delete(String[] taskid, HttpServletRequest request){
		for(String id : taskid){
			InspectionTask task = this.get(id);
			if(task == null){
				continue;
			}
			this.delete(task);
		}
		return null;
	}
	
	
	/**
	 * 添加巡查任务
	 * @param task
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError add(InspectionTask task,LoginUser loginUser, HttpServletRequest request){		
		Date now = new Date();
		task.setCreateTime(now);
		task.setUpdateTime(now);
		task.setCreateBy(loginUser.getLogin_name());
		task.setUpdateBy(loginUser.getLogin_name());
		task.setStatus("0");
		this.save(task);
		return null;
	}
	
	/** 
	 * 修改巡查任务
	 * @param task
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError update(InspectionTask task, LoginUser loginUser, HttpServletRequest request){
		InspectionTask old = getSafeEntity(task.getTaskId());
		String fields = "inspecCode,inspecName,taskdesc,remark";
		BeanUtil.setTargetFields(old, task, fields, true);
		old.setUpdateBy(loginUser.getLogin_name());
		old.setUpdateTime(new Date());
		Updater<InspectionTask> updater = new Updater<>(old, fields+",updateBy,updateTime");
		this.update(updater);
		return null;
	}
	
	/**
	 * 判断指定名称是否被使用
	 * @param inspecName
	 * @param taskId
	 * @return
	 */
	public boolean isNameDuplicate(String inspecName, String taskId){
		return isExists("inspecName", inspecName, "taskId", taskId);
	}
	
	/**
	 * 判断指定编号是否被使用
	 * @param inspecCode
	 * @param taskId
	 * @return
	 */
	public boolean isNoDuplicate(String inspecCode, String taskId){
		return isExists("inspecCode", inspecCode, "taskId", taskId);
	}
	
	
}
